package com.yang.core.model;

import lombok.Builder;
import lombok.Data;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 异常上下文
 */
@Data
@Builder
public class ExceptionContext {

    /**
     * 异常对象
     */
    private Exception exception;

    /**
     * 请求URL
     */
    private String url;

    /**
     * HTTP方法
     */
    private String method;

    /**
     * 是否应该重试
     */
    private boolean shouldRetry;

    /**
     * 附加属性
     */
    @Builder.Default
    private Map<String, Object> attributes = new ConcurrentHashMap<>();

    /**
     * 设置是否应该重试
     */
    public void setShouldRetry(boolean shouldRetry) {
        this.shouldRetry = shouldRetry;
    }

    /**
     * 设置属性
     */
    public void setAttribute(String name, Object value) {
        if (attributes == null) {
            attributes = new ConcurrentHashMap<>();
        }
        attributes.put(name, value);
    }

    /**
     * 获取属性
     */
    public Object getAttribute(String name) {
        return attributes != null ? attributes.get(name) : null;
    }
}